/*
 * $Id$
 */
package org.crazydays.wowarmory.page;


import java.io.InputStream;
import java.io.IOException;
import java.net.URI;

import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.crazydays.wowarmory.IPage;
import org.crazydays.wowarmory.IData;
import org.crazydays.wowarmory.WowArmoryException;

/**
 * AbstractPage
 */
public abstract class AbstractPage
	implements IPage
{
	/** log */
	protected static Log LOG = LogFactory.getLog(AbstractPage.class);

	/** base uri */
	protected URI baseUri;

	/** bytes */
	protected byte[] bytes;

	/**
	 * AbstractPage constructor.
	 *
	 * @param baseUri Base uri
	 */
	protected AbstractPage(URI baseUri)
	{
		setBaseUri(baseUri);
	}

	/**
	 * AbstractPage constructor.
	 *
	 * @param baseUri Base uri
	 */
	protected AbstractPage(String baseUri)
	{
		setBaseUri(baseUri);
	}

	/**
	 * Set base uri.
	 *
	 * @param baseUri Base uri
	 */
	protected void setBaseUri(URI baseUri)
	{
		this.baseUri = baseUri;
	}

	/**
	 * Get base uri.
	 *
	 * @return Base uri
	 */
	protected URI getBaseUri()
	{
		return baseUri;
	}

	/**
	 * Set base uri.
	 *
	 * @param baseUri Base uri
	 */
	protected void setBaseUri(String baseUri)
	{
		setBaseUri(URI.create(baseUri));
	}

	/**
	 * Set bytes.
	 *
	 * @param bytes Bytes
	 */
	public void setBytes(byte[] bytes)
	{
		this.bytes = bytes;
	}

	/**
	 * Get bytes.
	 *
	 * @return Bytes
	 */
	public byte[] getBytes()
	{
		return bytes;
	}

	/**
	 * Get get paramter.
	 *	
	 * @param parameter Parameter
	 * @param value Value
	 * @return Get parameter
	 */
	protected String getGetParameter(String parameter, String value)
	{
		StringBuffer buffer = new StringBuffer();

		buffer.append(parameter);
		buffer.append("=");
		buffer.append(value);

		return buffer.toString();
	}

	/**
	 * Get data.
	 *
	 * @param data Data
	 * @return Data
	 * @see IPage#getData(T)
	 */
	public <T extends IData> T getData(T data)
		throws WowArmoryException
	{
		if (data == null) {
			throw new IllegalArgumentException("data == null");
		}

		data.set(getBytes());

		return data;
	}

	/**
	 * ToString.
	 *
	 * @return String
	 * #see Object#toString()
	 */
	public String toString()
	{
		StringBuffer buffer = new StringBuffer();

		buffer.append("URI: ");
		buffer.append(getPageUri());

		return buffer.toString();
	}
}
